#include "stdafx.h"
#include "hash/hashstatistic.hpp"
#include <sstream>

namespace JHCPP
{
	namespace hash
	{
		CHashStatistic::CHashStatistic(
			UInt32 tableSize, 
			UInt32 numEntries, 
			UInt32 numZeroEntries, 
			UInt32 maxEntry, 
			std::vector<UInt32> details)
			:m_sizeOfTable(tableSize),
			m_numberOfEntries(numEntries),
			m_numZeroEntries(numZeroEntries),
			m_maxEntriesPerHash(maxEntry),
			m_detailedEntriesPerHash(details)
		{
		}

		CHashStatistic::~CHashStatistic()
		{
		}

		UInt32 CHashStatistic::maxPositionsOfTable() const
		{
			return m_sizeOfTable;
		}

		UInt32 CHashStatistic::numberOfEntries() const
		{
			return m_numberOfEntries;
		}

		UInt32 CHashStatistic::numberOfZeroPositions() const
		{
			return m_numZeroEntries;
		}

		double CHashStatistic::avgEntriesPerHash() const
		{
			return ((double) numberOfEntries()) / maxPositionsOfTable();
		}

		double CHashStatistic::avgEntriesPerHashExclZeroEntries() const
		{
			return ((double) numberOfEntries()) / (maxPositionsOfTable() - numberOfZeroPositions());
		}

		UInt32 CHashStatistic::maxEntriesPerHash() const
		{
			return m_maxEntriesPerHash;
		}

		const std::vector<UInt32> CHashStatistic::detailedEntriesPerHash() const
		{
			return m_detailedEntriesPerHash;
		}

		std::string CHashStatistic::toString() const
		{
			std::ostringstream str;
			str << "HashTable of size " << m_sizeOfTable << " containing " << m_numberOfEntries << " entries:\n";
			str << "  NumberOfZeroEntries: " << m_numZeroEntries << "\n";
			str << "  MaxEntry: " << m_maxEntriesPerHash << "\n";
			str << "  AvgEntry: " << avgEntriesPerHash() << ", excl Zero slots: " << avgEntriesPerHashExclZeroEntries() << "\n";
			str << "  DetailedStatistics: \n";
			for (std::size_t i = 0; i < m_detailedEntriesPerHash.size(); ++i)
			{
				// 10 entries per line
				if (i % 10 == 0)
				{
					str << "\n  " << i << ":";
				}
				str << " " << m_detailedEntriesPerHash[i];
			}
			str << "\n";
			str.flush();
			return str.str();
		}

	}//end of namespace hash
}//end of namespace JHCPP
